    var map;
    var gdir;
    var geocoder = null;
    var geo;
    var addressMarker;

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        //map.addControl(new GScaleControl());
        //map.addControl(new GOverviewMapControl());        
        
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        
        geo = new GClientGeocoder(); 
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

    function handleErrors()
    {           
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	   {	    
	   	  //do nothing
	   }
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	
	function returnObjById( id )
    {
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
    }
    
    function SetText(text, contr)
    {
        returnObjById(contr).value = text;
    }
    
    function SetLocation()
    { 
        returnObjById('messagefind').innerHTML = "";
                
        var from = returnObjById('txtFind').value;
        var to = returnObjById('txtFind').value;
        
        //multiple locations
        var panel = document.getElementById("messagefind");
        var txt = "txtFind";
        
        geo.getLocations(returnObjById(txt).value, function (result)
          {
            if (result.Status.code == G_GEO_SUCCESS)
            {
                 
                 // ===== If there was more than one result, "ask did you mean" on them all =====
                 if (result.Placemark.length > 1) 
                 { 
                    panel.innerHTML = "Did you mean:";
                    // Loop through the results
                    for (var i=0; i<result.Placemark.length; i++) 
                    {
                        var p = result.Placemark[i].Point.coordinates;
                        panel.innerHTML += "<br>"+(i+1)+": <a href=\"javascript:SetText('"+ result.Placemark[i].address+"', '"+txt+"'); \" >"+ result.Placemark[i].address +"</a>";
                    }
                 }                 
            }
            else 
            {
                var reason="Code "+result.Status.code;                
                alert('Could not find "'+returnObjById(txt).value+ '" ' + reason);
            }
          });       
        
        setDirections(from, to, 'en_US');
    }
    
    function FindLocation()
    { 
        returnObjById('messagefrom').innerHTML = "";
        returnObjById('messageto').innerHTML = "";
        
        var from = returnObjById('fromAddress').value;
        var to = returnObjById('toAddress').value;
        
        
        //multiple locations
        
        
        geo.getLocations(returnObjById('fromAddress').value, function (result)
          {
            var panel = document.getElementById("messagefrom");
            var txt = "fromAddress";
            if (result.Status.code == G_GEO_SUCCESS)
            {
                 
                 // ===== If there was more than one result, "ask did you mean" on them all =====
                 if (result.Placemark.length > 1) 
                 { 
                    panel.innerHTML = "Did you mean:";
                    // Loop through the results
                    for (var i=0; i<result.Placemark.length; i++) 
                    {
                        var p = result.Placemark[i].Point.coordinates;
                        panel.innerHTML += "<br>"+(i+1)+": <a href=\"javascript:SetText('"+ result.Placemark[i].address+"', '"+txt+"'); \" >"+ result.Placemark[i].address +"</a>";
                    }
                 }                 
            }
            else 
            {
                var reason="Code "+result.Status.code;                
                alert('Could not find "'+returnObjById(txt).value+ '" ' + reason);
            }
          });   
          
        //multiple locations        
        
        geo.getLocations(returnObjById('toAddress').value, function (result)
          {
          
            var panel = document.getElementById("messageto");
            var txt = "toAddress";
            
            if (result.Status.code == G_GEO_SUCCESS)
            {
                 
                 // ===== If there was more than one result, "ask did you mean" on them all =====
                 if (result.Placemark.length > 1) 
                 { 
                    panel.innerHTML = "Did you mean:";
                    // Loop through the results
                    for (var i=0; i<result.Placemark.length; i++) 
                    {
                        var p = result.Placemark[i].Point.coordinates;
                        panel.innerHTML += "<br>"+(i+1)+": <a href=\"javascript:SetText('"+ result.Placemark[i].address+"', '"+txt+"'); \" >"+ result.Placemark[i].address +"</a>";
                    }
                 }                 
            }
            else 
            {
                var reason="Code "+result.Status.code;                
                alert('Could not find "'+returnObjById(txt).value+ '" ' + reason);
            }
          });   
          
          
        setDirections(from, to, 'en_US');
    }